//package com.pengzy.core.cloud_security;
//
//import com.pengzy.comment.core.enums.PrErrorEnum;
//import com.pengzy.comment.core.enums.SecurityEnum;
//import com.pengzy.comment.core.jwt.JwtUtil;
//import com.pengzy.comment.web.exception.CustomException;
//import com.pengzy.comment.web.redis.RedisCache;
//import com.pengzy.supports.dto.LoginUser;
//import io.jsonwebtoken.Claims;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.http.HttpHeaders;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.authority.SimpleGrantedAuthority;
//import org.springframework.security.core.context.SecurityContext;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.web.server.context.ServerSecurityContextRepository;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import javax.annotation.Resource;
//import java.util.ArrayList;
//import java.util.Collection;
//import java.util.Map;
//import java.util.Objects;
//
///**
// * 用户信息上下文存储类
// * @author xiaozhi 2023/7/17
// */
//
//
//@Slf4j
//@Component
//public class SecurityRepository implements ServerSecurityContextRepository {
//
//    @Resource
//    private RedisCache redisCache;
//
//    @Override
//    public Mono<Void> save(ServerWebExchange exchange, SecurityContext context) {
//        return Mono.empty();
//    }
//
//    @Override
//    public Mono<SecurityContext> load(ServerWebExchange exchange) {
//        log.info("用户信息上下文存储类");
//        String token = exchange.getRequest().getHeaders().getFirst("token");
//        log.info(token);
//        if (StringUtils.isNotBlank(token)) {
//            try {
//                String user;
//                try {
//                    Claims claims = JwtUtil.parseJWT(token);
//                    user = claims.getSubject();
//                } catch (Exception e) {
//                    throw new CustomException(PrErrorEnum.ERROR_1003.getInfo(),PrErrorEnum.ERROR_1003.getCode());
//                }
//                String redisKey = SecurityEnum.LOGIN_KEY.getValue() + user;
//                SecurityUserDetails loginUser = redisCache.getCacheObject(redisKey);
//                if(Objects.isNull(loginUser)){
//                    throw new CustomException(PrErrorEnum.ERROR_1002.getInfo(),PrErrorEnum.ERROR_1002.getCode());
//                }
//                SecurityContext emptyContext = SecurityContextHolder.createEmptyContext();
//                Authentication authentication=new UsernamePasswordAuthenticationToken(loginUser,null,loginUser.getAuthorities());
//                emptyContext.setAuthentication(authentication);
//                return Mono.just(emptyContext);
//            }catch (Exception e) {
//                return Mono.empty();
//            }
//        }
//        return Mono.empty();
//    }
//
//}
